package com.safety360.app.adapter;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.safety360.app.Const;
import com.safety360.app.R;

public class DataAdapter extends BaseAdapter {

	private Context context;
	private ArrayList<HashMap<String, Object>> lstData;
	private TextView txtAge;
	private ImageView imgViewItem;
	private int holdPosition;
	private boolean isChanged = false;
	private boolean ShowItem = false;

	public DataAdapter(Context mContext, ArrayList<HashMap<String, Object>> listData) {
		this.context = mContext;
		this.lstData = listData;
	}

	@Override
	public int getCount() {
		return lstData.size();
	}

	@Override
	public Object getItem(int position) {
		return lstData.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public void showDropItem(boolean showItem) {
		this.ShowItem = showItem;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		convertView = LayoutInflater.from(context).inflate(
				R.layout.item_template_360_layout, null);
		imgViewItem = (ImageView) convertView
				.findViewById(R.id.imageView_ItemImage);
		txtAge = (TextView) convertView.findViewById(R.id.textView_ItemLabel);
		imgViewItem.setImageResource((Integer) lstData.get(position).get(Const.IMG_ID));
		txtAge.setText(lstData.get(position).get(Const.TXT_ITEM).toString());
		if (isChanged) {
			if (position == holdPosition) {
				if (!ShowItem) {
					convertView.setVisibility(View.INVISIBLE);
				}
			}
		}
		return convertView;
	}

	@SuppressWarnings("unchecked")
	public void exchange(int startPosition, int endPosition) {
		holdPosition = endPosition;
		Object startObject = getItem(startPosition);
		if (startPosition < endPosition) {
			lstData.add(endPosition + 1, (HashMap<String, Object>) startObject);
			lstData.remove(startPosition);
		} else {
			lstData.add(endPosition, (HashMap<String, Object>) startObject);
			lstData.remove(startPosition + 1);
		}
		isChanged = true;
		notifyDataSetChanged();
	}
}
